package com.meihua.shellapp.activity.wallet;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.meihua.shellapp.R;
import com.meihua.shellapp.adapter.personalcenter.BankAdapter;
import com.meihua.shellapp.base.BaseActivity;
import com.meihua.shellapp.base.BaseBean;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.bean.BankGetBean;
import com.meihua.shellapp.databean.BankDataBean;
import com.meihua.shellapp.dialog.BaseMessageDialog;
import com.meihua.shellapp.utils.ToastUtils;
import com.meihua.shellapp.utils.URL;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;


/**
 * 银行卡界面
 */
public class BankcardActivity extends BaseActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_top_left)
    ImageView mIVTopLeft;
    @Bind(R.id.bank_list)
    ListView bank_list;
    private List<BankDataBean> mBankList = new ArrayList<>();
    private BankAdapter adapter;

    private int type = 0;//   2,提现选择银行卡
    private BaseMessageDialog dialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_bankcard;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        type = getIntent().getIntExtra("type", 0);
        tvTitle.setVisibility(View.VISIBLE);
        tvTitle.setText("银行卡");
        mIVTopLeft.setVisibility(View.VISIBLE);
        mIVTopLeft.setOnClickListener(this);

        adapter = new BankAdapter(this, mBankList, type);
        bank_list.setAdapter(adapter);
        bank_list.setOnItemClickListener(this);
        bank_list.setOnItemLongClickListener(this);
    }

    @Override
    public void resumeLoadData() {
        super.resumeLoadData();
        OkGo.<String>post(URL.getUrl().GETBANKLIST)
                .params("mid", MyApplication.getInstance().getMId())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        BankGetBean bean = MyApplication.getInstance().getBeanForJson(BankGetBean.class, response.body());
                        if (bean == null) {
                            return;
                        }
                        if (bean.getCode() == 1) {
                            mBankList.clear();
                            mBankList.addAll(bean.getData());
                            adapter.notifyDataSetChanged();
                        } else {
                            ToastUtils.getInstance().showToast(bean.getMsg());
                        }
                    }
                });
    }

    /*条目点击事件*/
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

        if (type == 2) {
            Intent in = new Intent();
            in.putExtra("bankCard", (Serializable) mBankList.get(i));
            setResult(1, in);
            finish();
        } else if (type == 0 && i == adapter.getCount() - 1) {
            Intent in = new Intent(mContext, AddBankVerifyPwdActivity.class);
            startActivity(in);
        }
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int i, long l) {
        if (type == 0 && i < mBankList.size()) {
            dialog = new BaseMessageDialog.Builder(mContext)
                    .setClickSure(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            delBank(i);
                        }
                    }).setMessage("确定要删除吗？")
                    .show();
        }
        return true;
    }

    public void delBank(final int i) {
        OkGo.<String>post(URL.getUrl().DELBANK)
                .params("banks_id", mBankList.get(i).getBanks_id())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        BaseBean bean = MyApplication.getInstance().getBeanForJson(BaseBean.class, response.body());
                        if (bean == null) {
                            return;
                        }
                        if (bean.getCode() == 1) {
                            ToastUtils.getInstance().showToast(bean.getMsg());
                            mBankList.remove(i);
                            adapter.notifyDataSetChanged();
                        } else {
                            ToastUtils.getInstance().showToast(bean.getMsg());
                        }
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                        dialog.dismiss();
                    }
                });
    }
}
